using System;
using System.Collections.Generic;
using System.Text;
using Castle.DynamicProxy;
using Sio.Mdm.Types;
using Sio.Mdm.Utils;
using System.Diagnostics;
using Castle.Core.Interceptor;

namespace Sio.Mdm.Client
{
    /// <summary>
    /// Dummy instance that is returned in case acctual service is not found
    /// </summary>
    class DummyService : Server.IService, IInterceptor
    {
        #region Methods

        #region Public

        #region GetErrorReturnValue
        public static object GetErrorReturnValue(IInvocation invocation)
        {
            // Check wheater typed error can be returned
            if ( invocation.Method.ReturnType.IsSubclassOf(typeof(Result)) ||
                 invocation.Method.ReturnType == typeof(Result)
               )
            {
                return new ServiceNotAvailableError(invocation.Method.DeclaringType.ToString());
            }

            // Nothing we can do here
            return null;
        }
        #endregion

        #endregion

        #endregion

        #region IInterceptor Members
        [DebuggerStepThrough]
        /// <summary>
        /// Implemented explicitly since there is no need for this function
        /// to be visible to outside world
        /// </summary>
        /// <param name="invocation">Information on invocation</param>
        /// <param name="args">Arguments passed to function</param>
        /// <returns>This will always return <see cref="ServiceNotAvailableError"/></returns>
        object Intercept(IInvocation invocation, params object[] args)
        {
            Logger.WriteError("Call to unexisting service");
            Logger.WriteInformation("Method: {0}", invocation.Method.Name);

            return GetErrorReturnValue(invocation);
        }
        #endregion

        #region IInterceptor Members

        public void Intercept(IInvocation invocation)
        {
            invocation.ReturnValue = Intercept(invocation, invocation.Arguments);
        }

        #endregion
    }
}
